package com.canaan.business.service.agv.impl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import com.canaan.business.domain.agv.AgvTaskDispatchRecord;
import com.canaan.business.mapper.agv.AgvTaskDispatchRecordMapper;
import com.canaan.business.service.agv.IAgvTaskDispatchRecordService;
import org.springframework.stereotype.Service;
import com.canaan.common.utils.DateUtils;

/**
 * AGV数据调度记录Service业务层处理
 *
 * @author
 * @date 2023-03-02
 */
@Service
public class AgvTaskDispatchRecordServiceImpl implements IAgvTaskDispatchRecordService {
    @Resource
    private AgvTaskDispatchRecordMapper agvTaskDispatchRecordMapper;

    /**
     * 查询AGV数据调度记录
     *
     * @param id AGV数据调度记录主键
     * @return AGV数据调度记录
     */
    @Override
    public AgvTaskDispatchRecord selectAgvTaskDispatchRecordById(Long id) {
        return agvTaskDispatchRecordMapper.selectAgvTaskDispatchRecordById(id);
    }

    /**
     * 查询AGV数据调度记录列表
     *
     * @param agvTaskDispatchRecord AGV数据调度记录
     * @return AGV数据调度记录
     */
    @Override
    public List<AgvTaskDispatchRecord> selectAgvTaskDispatchRecordList(AgvTaskDispatchRecord agvTaskDispatchRecord) {
        return agvTaskDispatchRecordMapper.selectAgvTaskDispatchRecordList(agvTaskDispatchRecord);
    }

    /**
     * 新增AGV数据调度记录
     *
     * @param agvTaskDispatchRecord AGV数据调度记录
     * @return 结果
     */
    @Override
    public int insertAgvTaskDispatchRecord(AgvTaskDispatchRecord agvTaskDispatchRecord) {
        agvTaskDispatchRecord.setCreateTime(DateUtils.getNowDate());
        return agvTaskDispatchRecordMapper.insertAgvTaskDispatchRecord(agvTaskDispatchRecord);
    }

    /**
     * 批量新增AGV数据调度记录
     *
     * @param agvTaskDispatchRecordList AGV数据调度记录列表
     * @return 结果
     */
    @Override
    public int batchInsertAgvTaskDispatchRecord(List<AgvTaskDispatchRecord> agvTaskDispatchRecordList) {
        Date currerTime = DateUtils.getNowDate();
        for (AgvTaskDispatchRecord agvTaskDispatchRecord : agvTaskDispatchRecordList){
            agvTaskDispatchRecord.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<agvTaskDispatchRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > agvTaskDispatchRecordList.size()) {
                endIndex = agvTaskDispatchRecordList.size();
            }
            insertNum = insertNum + agvTaskDispatchRecordMapper.batchInsertAgvTaskDispatchRecord(agvTaskDispatchRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改AGV数据调度记录
     *
     * @param agvTaskDispatchRecord AGV数据调度记录
     * @return 结果
     */
    @Override
    public int updateAgvTaskDispatchRecord(AgvTaskDispatchRecord agvTaskDispatchRecord) {
        agvTaskDispatchRecord.setUpdateTime(DateUtils.getNowDate());
        return agvTaskDispatchRecordMapper.updateAgvTaskDispatchRecord(agvTaskDispatchRecord);
    }

    /**
     * 批量修改AGV数据调度记录
     *
     * @param agvTaskDispatchRecordList AGV数据调度记录列表
     * @return 结果
     */
    @Override
    public int batchUpdateAgvTaskDispatchRecord(List<AgvTaskDispatchRecord> agvTaskDispatchRecordList) {
        Date currerTime = DateUtils.getNowDate();
        for (AgvTaskDispatchRecord agvTaskDispatchRecord : agvTaskDispatchRecordList){
            agvTaskDispatchRecord.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<agvTaskDispatchRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > agvTaskDispatchRecordList.size()) {
                endIndex = agvTaskDispatchRecordList.size();
            }
            updateNum = updateNum + agvTaskDispatchRecordMapper.batchUpdateAgvTaskDispatchRecord(agvTaskDispatchRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除AGV数据调度记录
     *
     * @param ids 需要删除的AGV数据调度记录主键集合
     * @return 结果
     */
    @Override
    public int deleteAgvTaskDispatchRecordByIds(Long[] ids) {
        return agvTaskDispatchRecordMapper.deleteAgvTaskDispatchRecordByIds(ids);
    }

    /**
     * 删除AGV数据调度记录信息
     *
     * @param id AGV数据调度记录主键
     * @return 结果
     */
    @Override
    public int deleteAgvTaskDispatchRecordById(Long id) {
        return agvTaskDispatchRecordMapper.deleteAgvTaskDispatchRecordById(id);
    }
}
